//
//  AsyncImageView.m
//  phosphon.de
//
//  Created by Dirk Lieder on 30.09.09.
//  Copyright 2009 phosphon. All rights reserved.
//

#import "AsyncImageView.h"
#import "AsyncRead.h"
#import "getFilenameHD.h"

@implementation AsyncImageView

@synthesize myImage;
@synthesize asyncRead;

bool firstStart = true;

-(void) asyncImageLoad:(NSString*)loadImage{
	
	//NSLog(@"load image:%@",loadImage);
	
    // handle retina
    NSString *path;
    path = [PHOSPHON imagePostfix:loadImage];
    /*
    BOOL hasHighResScreen = NO;
    if ([UIScreen instancesRespondToSelector:@selector(scale)]) {
        CGFloat scale = [[UIScreen mainScreen] scale];
        if (scale > 1.0) {
            hasHighResScreen = YES;
        }
    }
    loadImage =[loadImage stringByReplacingOccurrencesOfString:@" " withString:@"-"];

    if(hasHighResScreen){
        path = [[NSBundle mainBundle] pathForResource:[loadImage stringByAppendingString:@"@2x"] ofType:@"jpg"]; 
    } else {
        path = [[NSBundle mainBundle] pathForResource:loadImage ofType:@"jpg"]; 
    }*/
    
    
    for (UIView *view in [self subviews]) {
        [view removeFromSuperview];
    }
    
    myImage = [[[UIImageView alloc] initWithImage:nil] autorelease];
   // myImage = [[UIImageView alloc] initWithImage:initWithContentsOfFile:path];
   
    myImage.frame = CGRectMake(0.0, 0.0, self.frame.size.width , self.frame.size.height);
    [self addSubview:myImage];
    
    
	// Abort active download
	[asyncRead abortDownload];
	
	// release old obj
	[asyncRead release];
	
	// Start cached download
	asyncRead = [[AsyncRead alloc] initWithUrl:path];
	[asyncRead downloadImage:self];

}


// set image
-(void) internetImageReady:(AsyncRead*)downloadedImage{	
   // NSLog(@"loaded image:");
	[myImage setImage:downloadedImage.Image];
}


- (void)dealloc {
	// Abort active download
	[asyncRead abortDownload];
	[asyncRead release];	
    if(myImage) [myImage release];
    
    [super dealloc];
}


@end
